package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.hydrant.FireHydrantAddRequest;
import com.example.fireassistant_server.dto.PointSearchFilter;
import com.example.fireassistant_server.dto.hydrant.FireHydrantUpdateRequest;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.mapper.FireHydrantMapper;
import com.example.fireassistant_server.service.FireHydrantService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.FireHydrantVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/hydrant")
public class FireHydrantController {

    UserService userService;

    FireHydrantService fireHydrantService;

    WechatUserService wechatUserService;

    FireHydrantMapper fireHydrantMapper;

    // 客户端添加消火栓 ；普通用户可以添加
    @PostMapping("/add/{user_id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    FireHydrantVo Add(@Validated @RequestBody FireHydrantAddRequest hydrantAddRequest, @PathVariable String user_id){
        User user = userService.get(user_id);
        return fireHydrantMapper.toVo(fireHydrantService.addHydrant(hydrantAddRequest, user));
    }
    // 微信端添加消火栓 ；普通用户可以添加
    @PostMapping("/weChat/add")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    FireHydrantVo weAdd(@Validated @RequestBody FireHydrantAddRequest hydrantAddRequest, @RequestHeader(value = "X-WX-OPENID") String openId){
        User user = wechatUserService.getCurrentUser(openId).getUser();
        return fireHydrantMapper.toVo(fireHydrantService.addHydrant(hydrantAddRequest,user));
    }

    //请求以 Body体为主，至少为{};没有用户权限的也可以访问；需要注册后使用
    @PostMapping("/search")
    @ApiOperation("用户检索")
    Page<FireHydrantVo> search(@Validated @RequestBody PointSearchFilter pointSearchFilter) {
        return fireHydrantService.search(pointSearchFilter).map(fireHydrantMapper::toVo);
    }

    // TODO 根据经纬度查找消火栓

    //删除消火栓以及与之相关文件；普通用户只能删除自己的录入的消火栓；admin可以删除辖区内的所有消火栓；谨慎使用；可以直接删除（有中间表）?
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    void delete(@PathVariable String id) {
        fireHydrantService.delete(id);
    }

    //根据id获取某个消火栓
    @GetMapping("/{id}")
    FireHydrantVo get(@PathVariable String id){
        System.out.println(fireHydrantService.get(id));
        return fireHydrantMapper.toVo(fireHydrantMapper.toDto(fireHydrantService.get(id)));
    }

    //根据消火栓id来更新
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    FireHydrantVo update(@PathVariable String id,
                  @Validated @RequestBody FireHydrantUpdateRequest fireHydrantUpdateRequest) {
        return fireHydrantMapper.toVo(fireHydrantService.update(id,fireHydrantUpdateRequest));
    }


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setFireHydrantService(FireHydrantService fireHydrantService) {
        this.fireHydrantService = fireHydrantService;
    }

    @Autowired
    public void setFireHydrantMapper(FireHydrantMapper fireHydrantMapper) {
        this.fireHydrantMapper = fireHydrantMapper;
    }
}
